﻿@model EC18_ESHOP.Models.CART
@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Customer.cshtml";
}
<br />
<div class="container">
    <div class="panel panel-default">
        <div class="panel-heading">
            @*<img />*@
            <h3 class="pull-left">Chi tiết giỏ hàng</h3>
            <a href="~/Home" class="pull-right"><< Tiếp tục mua hàng</a>
            <div class="clearfix"></div>
        </div>

        <div class="panel-body">
            @if (Model != null)
            {
                using (Html.BeginForm("Index", "CheckOut", FormMethod.Post, new { @id = "checkoutform" }))
                {
                    @Html.AntiForgeryToken()
                <table>
                    <thead>
                        <tr>
                            <th style="width: 500px;">Sản phẩm</th>
                            <th style="width: 120px;">Đơn giá</th>
                            <th style="width: 120px;">Số lượng</th>
                            <th style="width: 200px;">Giảm giá</th>
                            <th style="width: 120px;">Thành tiền</th>
                            <th style="width: 50px;">&nbsp;</th>
                        </tr>
                    </thead>


                    <tbody>
                        @foreach (var item in Model.CART_DETAIL)
                        {
                            <tr>
                                <td><a href="~/ProductDetail/Index/@item.ProductId">
                                    <img src="~/Images/Product/@item.PRODUCT.ProductId/01-s.jpg" alt="..." width="80">@item.PRODUCT.ProductName</a>
                                    @Html.Hidden("product", @item.PRODUCT.ProductName)
                                </td>
                                <td>@String.Format("{0:#,#} ₫", item.PRODUCT.ProductPrice)
                                    @Html.Hidden("price", @item.PRODUCT.ProductPrice)
                                </td>
                                <td>
                                    <select name="quantity">
                                        <option value="1">1</option>
                                        <option value="2">2</option>
                                        <option value="3">3</option>
                                        <option value="4">4</option>
                                        <option value="5">5</option>
                                        <option value="6">6</option>
                                        <option value="7">7</option>
                                        <option value="8">8</option>
                                        <option value="9">9</option>
                                        <option value="10">10</option>
                                    </select>
                                </td>
                                <td id="discount_@item.ProductId">0%</td>
                                <td>
                                    <span></span>
                                    @Html.Hidden("finalprice", @item.PRODUCT.ProductPrice, new { @id = @item.ProductId, @class = "test" })
                                </td>
                                <td>
                                    <a href="~/Cart/removeItem/@item.ProductId">
                                    <img src="~/Images/delete.png" alt="...">
                                    </a>
                                        </td>
                            </tr>
                        
                        }
                    </tbody>
                    <tfoot>
                        <tr>
                            <td colspan="3">&nbsp;</td>
                            <td colspan="1">
                                <strong>Tổng cộng</strong>
                            </td>
                            <td>
                                <div id="totalprice"><strong><span>0 ₫</span></strong></div>
                            </td>
                            <td>
                                <div id="totalpriceUSD"><strong><span>0 USD</span></strong></div>
                            </td>
                        </tr>
                    </tfoot>
                </table>

                
                <br />
                <br />
                <br />           
                <div class="float-center">

                    @*@Html.Hidden("arrayTotalPrice")*@

                    <button type="button" class="btn btn-primary center-block" id="btnSubmit">
                        Thanh toán >>
                    </button>
                    <div class="center-block"><strong>(Tài khoản sandbox paypal của người mua là: camapsunrang2014@gmail.com - pass: mothaiba) </strong></div>
                    @*<img src="https://www.paypal.com/en_US/i/btn/btn_xpressCheckout.gif" align="left" style="margin-right:7px;">*@
                </div>
                <br />
                }
            }
            else
            {
                <span>Giỏ hàng rỗng</span>
            }
        </div>

    </div>
</div>
@section scripts{
    <script type="text/javascript">
        

        $(function () {
            
            $.ajaxSetup({
                async: false
            });
            var USDtoVND = 1;
            $.getJSON("https://openexchangerates.org/api/latest.json?app_id=74f461a8eeae4f9bb8fdb99ef728b1cd", function (data) {
                USDtoVND = data.rates.VND;
            });

            
            loadPrice(function (totalprice) {
                $("#totalprice").text(totalprice.toLocaleString() + " ₫");
                var totalpriceUSD = totalprice / parseFloat(USDtoVND);
                totalpriceUSD = parseFloat(totalpriceUSD).toFixed(2);
                $("#totalpriceUSD").text(totalpriceUSD.toLocaleString() + " USD");
            });
            $.ajaxSetup({
                async: true
            });
            

            $("#btnSubmit").click(function () {
                var arrayTotalPrice = "";
                $("input[name*='finalprice']").each(function (index, element) {
                    // element == this
                    var vnd = $(element).val();
                    var usd = vnd / parseFloat(USDtoVND);
                    usd = parseFloat(usd).toFixed(2);
                    $(element).val(usd);
                    //arrayTotalPrice += usd + ",";
                });
                //$("#arrayTotalPrice").val(arrayTotalPrice);
                var form = $("#checkoutform");
                form.submit();
            });
        });

        function unload_form() {
            $.ajaxSetup({
                async: false
            });
            var USDtoVND = 1;
            $.getJSON("https://openexchangerates.org/api/latest.json?app_id=74f461a8eeae4f9bb8fdb99ef728b1cd", function (data) {
                USDtoVND = data.rates.VND;
            });


            loadPrice(function (totalprice) {
                $("#totalprice").text(totalprice.toLocaleString() + " ₫");
                var totalpriceUSD = totalprice / parseFloat(USDtoVND);
                totalpriceUSD = parseFloat(totalpriceUSD).toFixed(2);
                $("#totalpriceUSD").text(totalpriceUSD.toLocaleString() + " USD");
            });
            $.ajaxSetup({
                async: true
            });
        }

        function loadPrice(callback) {
            var totalprice = 0;
            $("input[name*='finalprice']").each(function (index, element) {
                var id = $(element).attr("id");
                var discount = 0;
                $.getJSON("Cart/getSaleOff/" + id, function (data) {
                    if (data.length > 0) {
                        //var SaleOffName = data[0].SaleOffName;
                        discount = data[0].Discount;
                        $("#discount_" + id).text(discount + "%");
                    }

                    var originprice = $(element).val();
                    var finalprice = originprice - (originprice * discount / 100);
                    $(element).closest("td").find("span").text(finalprice.toLocaleString() + " ₫");
                    $(element).val(finalprice);
                    totalprice += finalprice;
                });
            });
            if (typeof (callback) == 'function') {
                callback(totalprice);
            }
            //return totalprice;
        }
    </script>
}